<template>
	<view class="u-wrap">

		

		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item"
					:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
					@tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.classifyName}}</text>
				</view>
			</scroll-view>

			<scroll-view scroll-y class="right-box">
				<view class="page-view" v-for="(item,index) in list" :key="index" @click="detail(item)">
					<view class="class-item">
						<image class="item-menu-image" :src="item.cover" mode="aspectFill"></image>
						<view class="item-menu-name u-line-2" style="font-weight: bold;margin-bottom: 20rpx;">
							{{item.classifyName}}</view>
					</view>
				</view>
				
				<view v-if="!list.length" class="empty" style="margin-left: 100rpx;" >
					<u-empty text="暂无分类" mode='favor'></u-empty>
				</view>
			</scroll-view>
			
			
			
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabbar: [],
				list: [],
				goods_id:"",
				page: 1,
				count:"",
				ismore:false,
				keyword: "",
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
			}
		},
		
		
		async onLoad(options) {
			if(options.current){
				this.current=options.current
			}
		},
		
		async onShow() {
			
			this.keyword=""
			await this.getclass()
			this.detailClass()
		},
		methods: {
			
			carnum(){
				this.$u.api.carNum().then(res => {
				  this.count=res.data.cartNum	
				})
			},
			gocar(){
				uni.navigateTo({
					url:"../car/index"
				})
			},
			detail(item){
				uni.navigateTo({
					url:"./shopList?id="+item.id
				})
			},
			search() {
				this.page = 1;
				this.goods()
			},
			focus() {
				this.ismore = true
			},
			blur() {
				this.ismore = false
			},
			getclass() {
				return new Promise((resolve, reject) => {
					this.$u.post('/index/goodsClassify', {
                      goods_type:1,
					  
					}).then(res => {
						this.tabbar = res;
						resolve(res)
					}).catch(err => {

					})
				})
			},
			
			detailClass(){
				this.$u.post('/index/goodsClassify', {
				     parent_id:this.tabbar[this.current].id,
					 limit:20
				}).then(res => {
					this.list=res
					resolve(res)
				}).catch(err => {
				
				})
			},
			

			// 点击左边的栏目切换
			async swichMenu(index) {
				if (index == this.current) return;
				this.current = index;
				this.detailClass()
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			}
		}
	}
</script>

<style lang="scss" scoped>

    .carblock{
		position: fixed;
	   
		height: 80rpx;
		right: 0;
		bottom:200rpx;
		background: #FFFFFF;
		box-shadow: 0px 4rpx 6rpx rgba(209, 209, 209, 0.26);
		border-radius: 40rpx 0px 0px 40rpx;
		line-height: 80rpx;
		text-align: center;
		display: flex;
		align-items: center;
		padding:0 20rpx;
	}
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}
	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}


	.u-tab-view {
		width: 200rpx;
		height: 100%;
		
	}

	.u-tab-item {
		height: 110rpx;
	    background: #FFFFFF;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #037705;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid #037705;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
		display: flex;
	}

	.page-view {
		padding: 16rpx;
		width: 290rpx;
		
		display: inline-block;
	}

	.class-item {
        width: 280rpx;
		height: 220rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
		text-align: center;
	}

	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 25%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}

	.item-menu-image {
		width: 100%;
		height: 150rpx;
	}
</style>
